import thin_render from "../../../lib/thin_render.mjs";
import { confirm } from "../../../lib/dialog.mjs";
import { nearest_data } from "../../../lib/nearest.mjs";
import datarover from "../../../lib/datarover.mjs";

/**
 * 渲染下划线编辑器
 * @param {HTMLElement} td - 单元格元素
 * @param {Object} col - 列配置
 * @param {Object} template - 模板配置
 */
export function renderEditUnderline(td, col, template) {
  let underline = document.createElement("underline");
  td.appendChild(underline);
  render_value();

  underline.onclick = (e) => {
    if (template.debug) console.log(e);
    if (typeof col.edit === "function") {
      col.edit({
        element: td,
        data: nearest_data(td),
        done: function () {
          render_value();
        },
        changed: function (result) {
          Object.assign(nearest_data(td), result);
          if (template.rowchange) {
            template.rowchange({
              new_data: {},
              org_data: nearest_data(td),
            });
          }
          render_value();
        },
      });
    } else {
      let t =
        typeof col.edit === "object"
          ? col.edit
          : [
              { div: `${col.col}:` },
              {
                input: col.col,
                id: col.col,
                value: `[[${col.col}]]`,
                type: col.type,
                ajax: col.ajax,
                required: col.required,
                a: col.a,
              },
              "<br/>",
              "<br/>",
            ];

      confirm(t, nearest_data(td)).confirmed((result) => {
        Object.assign(nearest_data(td), result);
        if (template.rowchange) {
          template.rowchange({
            new_data: {},
            org_data: nearest_data(td),
          });
        }
        if (col.changed) {
          col.changed({
            new_data: result,
            org_data: nearest_data(td),
          });
        }
        render_value();
      });
    }
  };

  function render_value() {
    switch (typeof col.col) {
      case "string":
        let value = datarover(td, col.col);
        if (!value && value !== 0) {
        } else if (col.NumberFormat) {
          underline.innerText = new Intl.NumberFormat(
            "en-US",
            col.NumberFormat
          ).format(value);
        } else if (col.DateTimeFormat) {
          value = value?.replace(/\//g, "-");
          value = new Date(`${value}`);
          if (!isNaN(value.getTime())) {
            value = Date.UTC(
              value.getFullYear(),
              value.getMonth(),
              value.getDate(),
              value.getHours(),
              value.getMinutes(),
              value.getSeconds(),
              value.getMilliseconds()
            );
            underline.innerText = new Intl.DateTimeFormat(
              "default",
              col.DateTimeFormat
            ).format(value);
          } else {
            underline.innerText = value;
          }
        } else if (value || value === 0) {
          underline.innerText = value;
        }
        break;
    }
  }
}
